package com.anysoft.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.utils.DisplayUtil;

import ren.yale.android.cachewebviewlib.CacheWebView;

/**
 * Created by lty on 2018/2/12.
 * 浏览器Layout,封装以及初始化WebView
 */

public class BrowserLayout extends LinearLayout {
    private Context mContext;
    private HProgressBarLoading mProgressBar;
    private String mLoadUrl = "";
    private CacheWebView mWebView;
    private View errorView;

    public BrowserLayout(Context context) {
        this(context, null);
    }

    public BrowserLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        setOrientation(VERTICAL);
        mContext = context;
        mProgressBar = initProgressBar(context);
        addView(mProgressBar, LayoutParams.MATCH_PARENT, mProgressBar.getProgressBarHeight());
        Log.e("====>", "init: " + mProgressBar.getProgressBarHeight());
        mWebView = new CacheWebView(context);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1);
        addView(mWebView, params);
        errorView = initErrorView(context);
        addView(errorView, new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
    }

    private View initErrorView(Context context) {
        View errorView = LayoutInflater.from(context).inflate(R.layout.layout_web_error, null);
        TextView tv_item_web_error = (TextView) errorView.findViewById(R.id.tv_item_web_error);
        tv_item_web_error.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                refresh(mLoadUrl);
            }
        });
        return errorView;
    }

    @NonNull
    private HProgressBarLoading initProgressBar(Context context) {
        HProgressBarLoading progressBar = new HProgressBarLoading(context);
        progressBar.setMax(100);
        progressBar.setCurProgress(0);
        progressBar.setProgressBarHeight(DisplayUtil.dp2px(mContext, 1));
        return progressBar;
    }

    /**
     * 设置加载进度
     *
     * @param progress
     */
    public void setCurProgress(int progress) {
        if (mProgressBar != null) {
            mProgressBar.setCurProgress(progress);
        }
    }

    /**
     * 设置当前网页地址
     *
     * @param url
     */
    public void setLoadUrl(String url) {
        mLoadUrl = url;
    }

    /**
     * 刷新网页
     *
     * @param url
     */
    public void refresh(String url) {
        mWebView.reload();
    }

    /**
     * 是否能回退
     *
     * @return
     */
    public boolean canGoBack() {
        return null != mWebView ? mWebView.canGoBack() : false;
    }

    /**
     * 是否能前进
     *
     * @return
     */
    public boolean canGoForward() {
        return null != mWebView ? mWebView.canGoForward() : false;
    }


    /**
     * 回退
     */
    public void goBack() {
        if (null != mWebView) {
            mWebView.goBack();
        }
    }

    /**
     * 前进
     */
    public void goForward() {
        if (null != mWebView) {
            mWebView.goForward();
        }
    }

    public void setShowErrorView(boolean isLoadError) {
        if (isLoadError) {
            errorView.setVisibility(VISIBLE);
            mWebView.setVisibility(GONE);
        } else {
            errorView.setVisibility(GONE);
            mWebView.setVisibility(VISIBLE);
        }
    }

    public CacheWebView getWebView() {
        return mWebView != null ? mWebView : null;
    }

    public void destory() {
        //如此才能真正清除，不会导致内存泄漏
        if (mWebView != null) {
            //加载空内容
            mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebView.removeAllViews();
            ((ViewGroup) mWebView.getParent()).removeView(mWebView);
            mWebView.destroy();
            mWebView = null;
        }
    }
}
